package com.wyp.demo.pattern.proxy.dynamic;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * JDK 动态代理,实现InvocationHandler接口,相当于横切逻辑实现 AOP中的aspect切面
 *
 * @author: Oliver
 * @time: 2020/12/20/10:39
 */
public class AlipayInvocationHandler implements InvocationHandler {

    /**
     * 目标代理对象,在反射调用方法时,需要,以构造方法方式进行传入
     */
    Object target;

    public AlipayInvocationHandler(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();
        Object invoke = method.invoke(target, args);
        after();
        return invoke;
    }

    private void before() {
        System.out.println("调用开始之前,从卡里取钱");
    }

    private void after() {
        System.out.println("调用结束之后,扣钱成功");
    }
}
